import { Controller, Get, Logger } from '@nestjs/common';
import { AppService } from './app.service';
import { Ctx, EventPattern, MessagePattern, Payload, RmqContext } from '@nestjs/microservices';
import { Observable, of } from 'rxjs';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @EventPattern('emit')
  async handleUserCreated(
    @Payload() data: string,
    @Ctx() context: RmqContext,
  ): Promise<Observable<string>> {

    Logger.debug(data, 'EventPattern');
    return of(data);
  }

  @MessagePattern({ cmd: 'length' })
  async sum(
    @Payload() data: string,
    @Ctx() context: RmqContext,
  ): Promise<string> {

    Logger.debug(data, 'MessagePattern');
    return of(data + 'length:' + data.length.toString()).toPromise();
  }
}
